package com.example.server.controller;


import com.ai.AIProto.ScriptInitializerRequest;
import com.ai.AIProto.ScriptInitializerResponse;
import com.ai.AIServiceGrpc;
import com.ai.AIProto.AIRequest;
import com.ai.AIProto.AIResponse;



import com.example.server.common.AIRequestDTO;
import com.example.server.common.AIResponseDTO;

import com.example.server.common.ScriptInitializerDTO;
import com.example.server.common.ScriptInitializerResponseDTO;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/AIClient")
public class AIController {

    @GrpcClient("my-service")
    private AIServiceGrpc.AIServiceBlockingStub stub;

    @PostMapping("/initialize")
    public ScriptInitializerResponseDTO Script_Initialize(@RequestBody ScriptInitializerDTO requestDto) {

        // 将 DTO 转换为 gRPC 请求
        ScriptInitializerRequest request = ScriptInitializerRequest.newBuilder()
                .setConversationID(requestDto.getConversationID())
                .setScriptId(requestDto.getScriptId())
                .build();

        // 调用 gRPC 服务
        ScriptInitializerResponse response = stub.initializeScript(request);

        // 将 gRPC 响应转换为 DTO
        ScriptInitializerResponseDTO responseDTO = new ScriptInitializerResponseDTO();
        responseDTO.setMessage(response.getMessage());
        responseDTO.setStatus(response.getStatus());

        return responseDTO;
    }

    @PostMapping("/ask")
    public AIResponseDTO ask(@RequestBody AIRequestDTO requestDto) {
        // 将 DTO 转换为 gRPC 请求
        AIRequest request = AIRequest.newBuilder()
                .setConversationID(requestDto.getConversationID())
                .setUserID(requestDto.getUserID())
                .setContent(requestDto.getContent())
                .setStatus(requestDto.getStatus())
                .build();
        // 调用 gRPC 服务
        AIResponse response = stub.ask(request);
        // 将 gRPC 响应转换为 DTO
        AIResponseDTO responseDTO = new AIResponseDTO();
        responseDTO.setCode(response.getCode());
        responseDTO.setMessage(response.getMessage());
        responseDTO.setData(response.getData());

        return responseDTO;
    }

}
